<?php

namespace app\home\controller;

use app\common\controller\Home;
use \jwt\JWT;
use \hashids\Hashids;

class Test extends Home
{
    public function index()
    {
        $hashids = new Hashids('plain', 7);
        // 加密  
        $str = $hashids->encode(1);
        // 解密  
        $ids = $hashids->decode($str);

        print_r($str);
        print_r($ids);
    }

    public function jwt()
    {

        $key = "example_key";
        $token = array(
            "iss" => "http://example.org",
            "aud" => "http://example.com",
            "iat" => 1356999524,
            "nbf" => 1357000000
        );

        /**
         * IMPORTANT:
         * You must specify supported algorithms for your application. See
         * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
         * for a list of spec-compliant algorithms.
         */
        $str = JWT::encode($token, $key);
        $decoded = JWT::decode($str, $key, array('HS256'));

        print_r($str);
        print_r($decoded);
    }
    
    public function all() {
        $Url = zanpiancms_param_url();
        $array_detail = $this->get_cache_detail($Url);
        if($array_detail) {
            $data = playlist_json($array_detail["read"]["vod_play_list"]);
            return json(['data' => $data]);
        } else {
            return json(['data' => array()]);
        }
    }

    //从数据库获取数据
	private function get_cache_detail($param)
	{
		if (empty($param['id']) && empty($param['pinyin'])) {
			return false;
		}
		//优先读取缓存数据
		if ($GLOBALS['config']['cache']['data_cache_vod']) {
			$array_detail = Cache::get('data_cache_vod_' . $param['id'] . $param['pinyin']);
			if ($array_detail) {
				return $array_detail;
			}
		}
		//未中缓存则从数据库读取
		$where = array();
		if (!empty($param['id'])) {
			$where['vod_id'] = $param['id'];
		}
		if (!empty($param['pinyin'])) {
			$where['vod_letters'] = $param['pinyin'];
		}
		$where['vod_status'] = array('eq', 1);
		$data = db('vod')->alias('vod')->join('actor actor', 'actor.actor_vid =vod.vod_id', 'LEFT')->join('story story', 'story.story_vid =vod.vod_id', 'LEFT')->where($where)->find();
		if (!empty($data)) {
			//解析标签
			$array_detail = $this->Lable_Vod_Read($data, 1);
			if ($GLOBALS['config']['cache']['data_cache_vod']) {
				Cache::tag('data_vod')->set('data_cache_vod_' . $param['id'] . $param['pinyin'], $array_detail, intval($GLOBALS['config']['cache']['data_cache_vod']));
			}
			return $array_detail;
		}
		return false;
	}
}
